from typing import List

def split_text(text: str, method: str = 'char', size: int = 500) -> List[str]:
    """
    分割文本为多个片段
    
    Args:
        text: 要分割的文本
        method: 分割方法 ('char', 'word', 'sentence')
        size: 每个片段的大小
    
    Returns:
        分割后的文本片段列表
    """
    if method == 'char':
        return split_by_char(text, size)
    elif method == 'word':
        return split_by_word(text, size)
    elif method == 'sentence':
        return split_by_sentence(text, size)
    else:
        return split_by_char(text, size)  # 默认使用字符分割

def split_by_char(text: str, size: int) -> List[str]:
    """按字符数分割文本"""
    return [text[i:i + size] for i in range(0, len(text), size)]

def split_by_word(text: str, size: int) -> List[str]:
    """按词数分割文本"""
    words = text.split()
    chunks = []
    current_chunk = []
    current_size = 0
    
    for word in words:
        current_chunk.append(word)
        current_size += 1
        
        if current_size >= size:
            chunks.append(' '.join(current_chunk))
            current_chunk = []
            current_size = 0
    
    if current_chunk:
        chunks.append(' '.join(current_chunk))
    
    return chunks

def split_by_sentence(text: str, size: int) -> List[str]:
    """按句子数分割文本"""
    # 简单的句子分割，可以根据需要改进
    sentences = text.replace('。', '。\n').replace('！', '！\n').replace('？', '？\n').split('\n')
    sentences = [s.strip() for s in sentences if s.strip()]
    
    chunks = []
    current_chunk = []
    current_size = 0
    
    for sentence in sentences:
        current_chunk.append(sentence)
        current_size += 1
        
        if current_size >= size:
            chunks.append(' '.join(current_chunk))
            current_chunk = []
            current_size = 0
    
    if current_chunk:
        chunks.append(' '.join(current_chunk))
    
    return chunks 